/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.om.constans.OmCommonConstants;
import com.excesys.exsecs.om.entity.OmICheckPoint;
import com.excesys.exsecs.om.entity.OmICheckSheetDetail;
import com.excesys.exsecs.om.mapper.OmICheckPointMapper;
import com.excesys.exsecs.om.mapper.OmICheckSheetDetailMapper;
import com.excesys.exsecs.om.mapper.OmICheckSheetMapper;
import com.excesys.exsecs.om.service.OmICheckSheetDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 巡检详细信息
 *
 * @author pig code generator
 * @date 2023-03-02 19:43:51
 */
@Service
@RequiredArgsConstructor
public class OmICheckSheetDetailServiceImpl extends ServiceImpl<OmICheckSheetDetailMapper, OmICheckSheetDetail>
		implements OmICheckSheetDetailService {

	private final OmICheckSheetDetailMapper omICheckSheetDetailMapper;

	private final OmICheckPointMapper omICheckPointMapper;

	private final OmICheckSheetMapper omICheckSheetMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveDetail(List<OmICheckSheetDetail> detailList) {

		String checkNo = "";
		Integer pointId = null;
		for (OmICheckSheetDetail omICheckSheetDetail : detailList) {
			String devStatus = omICheckSheetDetail.getDevStatus();
			pointId = omICheckSheetDetail.getPointId();
			if (OmCommonConstants.DEV_STATUS_1.equals(devStatus)) {

				OmICheckPoint omICheckPoint = omICheckPointMapper.selectById(pointId);
				omICheckPoint.setWorkStatus(OmCommonConstants.WORK_STATUS_1);
				omICheckPointMapper.updateById(omICheckPoint);
			}
			checkNo = omICheckSheetDetail.getCheckNo();
		}

		omICheckSheetDetailMapper.delete(Wrappers.<OmICheckSheetDetail>update().lambda()
				.eq(OmICheckSheetDetail::getCheckNo, checkNo).eq(OmICheckSheetDetail::getPointId, pointId));
		// 保存巡检详细信息
		Optional.ofNullable(detailList).ifPresent(detailLists -> {
			detailLists.stream().map(omICheckSheetDetail -> {
				// omICheckSheetDetail.setPlanId(omICheckPlan.getPlId());
				return omICheckSheetDetail;
			}).forEach(omICheckSheetDetailMapper::insert);
		});

		return Boolean.TRUE;
	}

}
